package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.RptPatentDataAreaInfo;
import com.baitengsoft.patentapimonitor.api.model.dto.WebAnalysisExportDto;
import com.baitengsoft.patentapimonitor.api.service.RptPatentDataAreaService;
import com.baitengsoft.patentapimonitor.api.service.WebExcelExportService;
import com.baitengsoft.patentapimonitor.common.utils.DateUtils;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.WebExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author Chen 2018/1/30
 */
@RestController
@RequestMapping("api/v1/web/excel")
@Api(value = "前台-专利创造指标分析-导出Excel",description = "专利创造指标分析")
public class WebStatisticExcelExportController extends AbstractController {
    @Autowired
    private RptPatentDataAreaService rptPatentDataAreaService;
    @Autowired
    private WebExcelExportService webExcelExportService;

    /**
     * 专利创造指标分析导出
     */
    @ApiOperation(value = "专利创造指标分析-有效发明/year 1-今年，0-去年")
    @GetMapping(value="/validipat/{year}")
    public Res getValidIPatList(@PathVariable(value = "year") Integer year, HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getValidIPatListByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos, response, DateUtils.getCurrentYear()+"年有效发明.xls", DateUtils.getCurrentYear()+"年有效发明");

    }

    /**
     * 万人有效发明专利拥有量，获取指定平台下区域的当前万人有效发明专利拥有量
     */
    @ApiOperation(value = "万人有效发明专利拥有量/year 1-今年，0-去年")
    @GetMapping("/millionipat/{year}")
    public Res getMillionIPatList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getMillionIPatByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年当前万人有效发明专利拥有量.xls", DateUtils.getCurrentYear()+"年当前万人发明专利拥有量");
    }


    /**
     * 专利申请
     */
    @ApiOperation(value = "专利申请/year 1-今年，0-去年")
    @GetMapping("/patapply/{year}")
    public Res getPatApplyList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getPatApplyByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年专利申请量.xls",DateUtils.getCurrentYear()+"年专利申请量");
    }

    /**
     * 发明专利申请
     */
    @ApiOperation(value = "发明专利申请/year 1-今年，0-去年")
    @GetMapping("/ipatapply/{year}")
    public Res getIpatApplyList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getIpatApplyByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年发明专利申请.xls",DateUtils.getCurrentYear()+"年发明专利申请");
    }

    /**
     * 专利授权
     */
    @ApiOperation(value = "专利授权/year 1-今年，0-去年")
    @GetMapping("/patauthorize/{year}")
    public Res getPatAuthorizeList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getPatAuthorizeByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年"+"专利授权量.xls",DateUtils.getCurrentYear()+"年专利授权量");
    }

    /**
     * 万人专利授权
     */
    @ApiOperation(value = "万人专利授权/year 1-今年，0-去年")
    @GetMapping("/wrpatauthorize/{year}")
    public Res getWRPatAuthorizeList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getWRPatAuthorizeByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年万人专利授权.xls",DateUtils.getCurrentYear()+"万人专利授权");
    }

    /**
     * 发明专利授权
     */
    @ApiOperation(value = "发明专利授权/year 1-今年，0-去年")
    @GetMapping("/ipatauthorize/{year}")
    public Res getIpatAuthorizeList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getIpatAuthorizeByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年发明专利授权.xls",DateUtils.getCurrentYear()+"年发明专利授权");
    }

    /**
     * 发明专利授权
     */
    @ApiOperation(value = "万人发明专利授权/year 1-今年，0-去年")
    @GetMapping("/wripatauthorize/{year}")
    public Res getWRIpatAuthorizeList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getWRIpatAuthorizeByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年万人发明专利授权.xls",DateUtils.getCurrentYear()+"年万人发明专利授权");
    }


    /**
     * 万人有效专利拥有量
     */
    @ApiOperation(value = "万人有效专利拥有量/year 1-今年，0-去年")
    @GetMapping("/wryxpatent/{year}")
    public Res getWRYXPatentList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getWRYXPatentByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年万人有效专利拥有量.xls", DateUtils.getCurrentYear()+"年万人有效专利拥有量");
    }

    /**
     * 实质审查
     */
    @ApiOperation(value = "实质审查/year 1-今年，0-去年")
    @GetMapping("/szsc/{year}")
    public Res getSZSCList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getSZSCByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年实质审查.xls",DateUtils.getCurrentYear()+"年实质审查");
    }

    /**
     * 有效专利
     */
    @ApiOperation(value = "有效专利/year 1-今年，0-去年")
    @GetMapping("/yxpatent/{year}")
    public Res getYXPatentList(@PathVariable(value = "year") Integer year,HttpServletResponse response){
        if (year ==null){
            year = 0;
        }
        if (year !=1 && year!=0){
            return Res.error("请求参数有误");
        }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("登录信息失效");
        }
        List<RptPatentDataAreaInfo> infos = rptPatentDataAreaService.getYXPatentByYear(userInfo.getPlatformid(), year);
        List<WebAnalysisExportDto> webDtos=webExcelExportService.getWebAnalysisExportDto(infos);
        return WebExcelUtils.exportExcelFile(webDtos,response, DateUtils.getCurrentYear()+"年有效专利.xls",DateUtils.getCurrentYear()+"年有效专利");
    }
}
